
import { View } from '@tarojs/components';
import Taro, { useState } from '@tarojs/taro';
import MMButton from '~/modules/@wmeimob/taro-design/src/components/button';
import * as styles from './index.module.less';
import {isPhone,taost, throttle } from "~/common/common"
interface CountProps{
    phone?:number,
}
const CountDown:Taro.FC<CountProps>=(props:CountProps)=>{
    const [getmsg, setCode] = useState("获取验证码")
    const [changeMsg, setChangeMsg] = useState(true)
    const [obj, setObj] = useState({
        phone:props.phone,
        code: null
    })
     // 获取验证码
     async function getCode() {
        if (!obj.phone) {
            taost.success("请输入手机号")
            return
        }
        if (!isPhone(obj.phone )) {
            taost.success("手机号格式错误")
            return
        }
        if (!changeMsg) {
            return;
        }
        // 放到接口成功的回调下
        taost.success("发送成功")
        let time = 60;
        const timer = setInterval(() => {
            setCode(time + 's')
            setChangeMsg(false)
            time--;
            if (time < 0) {
                clearInterval(timer);
                setCode('获取验证码')
                setChangeMsg(true)
            }
        }, 1000);
    }
    return(
        <View className={styles.item}>
            <MMButton>
                 <View onClick={throttle(() =>getCode())}>{getmsg}</View>
            </MMButton>
        </View>
    )
}
export default CountDown
